<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8"/>
<title>C#版Windows服务安装卸载小工具-附源码 - 韩天伟 - 博客园</title>
<link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=Jsknfrse2BShd-7DrxM42RyulObn44e4JjkI6-WX6zk1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="/skins/Custom/bundle-Custom.css?v=Z683z9azGWin6jOfOyAHK6FgPVxm_nmCnl3EFztc2eE1"/>
<link type="text/css" rel="stylesheet" href="/blog/customcss/55427.css?v=II%2fnvlWz%2faipav3%2bIlqrPfyoC5A%3d"/>
<link title="RSS" type="application/rss+xml" rel="alternate" href="http://www.cnblogs.com/hantianwei/rss"/>
<link title="RSD" type="application/rsd+xml" rel="EditURI" href="http://www.cnblogs.com/hantianwei/rsd.xml"/>
<link type="application/wlwmanifest+xml" rel="wlwmanifest" href="http://www.cnblogs.com/hantianwei/wlwmanifest.xml"/>
<script src="http://common.cnblogs.com/script/jquery.js" type="text/javascript"></script>  
<script type="text/javascript">var currentBlogApp = 'hantianwei', cb_enable_mathjax=false;</script>
<script src="/bundles/blog-common.js?v=V3tkfsuahqfUzgmqZVwj2QjsxodIvgDfvPKdn8yDuMY1" type="text/javascript"></script>
</head>
<body>
<a name="top"></a>

<!--done-->
<div id="home">
<div id="header">
	<div id="blogTitle">
	<a id="lnkBlogLogo" href="http://www.cnblogs.com/hantianwei/"><img id="blogLogo" src="/Skins/custom/images/logo.gif" alt="返回主页" /></a>			
		
<!--done-->
<h1><a id="Header1_HeaderTitle" class="headermaintitle" href="http://www.cnblogs.com/hantianwei/">韩天伟</a></h1>
<h2>每天比别人多工作、学习两个小时，就等于比别人多活了两个小时！~</h2>



		
	</div><!--end: blogTitle 博客的标题和副标题 -->
	<div id="navigator">
		
<ul id="navList">
<li></li>
<li><a id="MyLinks1_MyHomeLink" class="menu" href="http://www.cnblogs.com/hantianwei/">首页</a></li>
<li><a id="MyLinks1_NewPostLink" class="menu" rel="nofollow" href="http://i.cnblogs.com/EditPosts.aspx?opt=1">新随笔</a></li>
<li><a id="MyLinks1_ContactLink" class="menu" rel="nofollow" href="http://msg.cnblogs.com/send/%E9%9F%A9%E5%A4%A9%E4%BC%9F">联系</a></li>
<li><a id="MyLinks1_Syndication" class="menu" href="http://www.cnblogs.com/hantianwei/rss">订阅</a>
<!--<a id="MyLinks1_XMLLink" class="aHeaderXML" href="http://www.cnblogs.com/hantianwei/rss"><img src="http://www.cnblogs.com/images/xml.gif" alt="订阅" /></a>--></li>
<li><a id="MyLinks1_Admin" class="menu" rel="nofollow" href="http://i.cnblogs.com/">管理</a></li>
</ul>
		<div class="blogStats">
			
			
<!--done-->
随笔- 287&nbsp;
文章- 1&nbsp;
评论- 123&nbsp;

			
		</div><!--end: blogStats -->
	</div><!--end: navigator 博客导航栏 -->
</div><!--end: header 头部 -->

<div id="main">
	<div id="mainContent">
	<div class="forFlow">
		
<div id="post_detail">
<!--done-->
<div id="topics">
	<div class = "post">
		<h1 class = "postTitle">
			<a id="cb_post_title_url" class="postTitle2" href="http://www.cnblogs.com/hantianwei/p/4799434.html">C#版Windows服务安装卸载小工具-附源码</a>
		</h1>
		<div class="clear"></div>
		<div class="postBody">
			<div id="cnblogs_post_body"><h3><span style="font-size: 2em; line-height: 1.5;">前言</span></h3>
<p>在我们的工作中，经常遇到Windows服务的安装和卸载，在之前公司也普写过一个WinForm程序选择安装路径，这次再来个小巧灵活的控制台程序，不用再选择，只需放到需要安装服务的目录中运行就可以实现安装或卸载。</p>
<h1>开发思路</h1>
<p>1、由于系统的权限限制，在运行程序时需要以管理员身份运行</p>
<p>2、因为需要实现安装和卸载两个功能，在程序运行时提示本次操作是安装还是卸载 &nbsp;需要输入 1 或 2</p>
<p>3、接下来程序会查找当前目录中的可执行文件并过滤程序本身和有时我们复制进来的带有vhost的文件，并列出列表让操作者选择（一般情况下只有一个）</p>
<p>4、根据用户所选进行安装或卸载操作</p>
<p>5、由于可能重复操作，需要递归调用一下</p>
<h1>具体实现</h1>
<p>首先们要操作服务，需要用 &nbsp;System.ServiceProcess 来封装实现类</p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: #008080;">  1</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System;
</span><span style="color: #008080;">  2</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Collections;
</span><span style="color: #008080;">  3</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Configuration.Install;
</span><span style="color: #008080;">  4</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Linq;
</span><span style="color: #008080;">  5</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System.ServiceProcess;
</span><span style="color: #008080;">  6</span> 
<span style="color: #008080;">  7</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> AutoInstallUtil
</span><span style="color: #008080;">  8</span> <span style="color: #000000;">{
</span><span style="color: #008080;">  9</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> SystemServices
</span><span style="color: #008080;"> 10</span> <span style="color: #000000;">    {
</span><span style="color: #008080;"> 11</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span>
<span style="color: #008080;"> 12</span>         <span style="color: #808080;">///</span><span style="color: #008000;"> 打开系统服务
</span><span style="color: #008080;"> 13</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span>
<span style="color: #008080;"> 14</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;param name="serviceName"&gt;</span><span style="color: #008000;">系统服务名称</span><span style="color: #808080;">&lt;/param&gt;</span>
<span style="color: #008080;"> 15</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;returns&gt;&lt;/returns&gt;</span>
<span style="color: #008080;"> 16</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">bool</span> SystemServiceOpen(<span style="color: #0000ff;">string</span><span style="color: #000000;"> serviceName)
</span><span style="color: #008080;"> 17</span> <span style="color: #000000;">        {
</span><span style="color: #008080;"> 18</span>             <span style="color: #0000ff;">try</span>
<span style="color: #008080;"> 19</span> <span style="color: #000000;">            {
</span><span style="color: #008080;"> 20</span>                 <span style="color: #0000ff;">using</span> (<span style="color: #0000ff;">var</span> control = <span style="color: #0000ff;">new</span><span style="color: #000000;"> ServiceController(serviceName))
</span><span style="color: #008080;"> 21</span> <span style="color: #000000;">                {
</span><span style="color: #008080;"> 22</span>                     <span style="color: #0000ff;">if</span> (control.Status !=<span style="color: #000000;"> ServiceControllerStatus.Running)
</span><span style="color: #008080;"> 23</span> <span style="color: #000000;">                    {
</span><span style="color: #008080;"> 24</span> <span style="color: #000000;">                        control.Start();
</span><span style="color: #008080;"> 25</span> <span style="color: #000000;">                    }
</span><span style="color: #008080;"> 26</span> <span style="color: #000000;">                }
</span><span style="color: #008080;"> 27</span>                 <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 28</span> <span style="color: #000000;">            }
</span><span style="color: #008080;"> 29</span>             <span style="color: #0000ff;">catch</span>
<span style="color: #008080;"> 30</span> <span style="color: #000000;">            {
</span><span style="color: #008080;"> 31</span>                 <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 32</span> <span style="color: #000000;">            }
</span><span style="color: #008080;"> 33</span> <span style="color: #000000;">        }
</span><span style="color: #008080;"> 34</span> 
<span style="color: #008080;"> 35</span> 
<span style="color: #008080;"> 36</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span>
<span style="color: #008080;"> 37</span>         <span style="color: #808080;">///</span><span style="color: #008000;"> 关闭系统服务
</span><span style="color: #008080;"> 38</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span>
<span style="color: #008080;"> 39</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;param name="serviceName"&gt;</span><span style="color: #008000;">系统服务名称</span><span style="color: #808080;">&lt;/param&gt;</span>
<span style="color: #008080;"> 40</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;returns&gt;&lt;/returns&gt;</span>
<span style="color: #008080;"> 41</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">bool</span> SystemServiceClose(<span style="color: #0000ff;">string</span><span style="color: #000000;"> serviceName)
</span><span style="color: #008080;"> 42</span> <span style="color: #000000;">        {
</span><span style="color: #008080;"> 43</span>             <span style="color: #0000ff;">try</span>
<span style="color: #008080;"> 44</span> <span style="color: #000000;">            {
</span><span style="color: #008080;"> 45</span>                 <span style="color: #0000ff;">using</span> (<span style="color: #0000ff;">var</span> control = <span style="color: #0000ff;">new</span><span style="color: #000000;"> ServiceController(serviceName))
</span><span style="color: #008080;"> 46</span> <span style="color: #000000;">                {
</span><span style="color: #008080;"> 47</span> 
<span style="color: #008080;"> 48</span>                     <span style="color: #0000ff;">if</span> (control.Status ==<span style="color: #000000;"> ServiceControllerStatus.Running)
</span><span style="color: #008080;"> 49</span> <span style="color: #000000;">                    {
</span><span style="color: #008080;"> 50</span> <span style="color: #000000;">                        control.Stop();
</span><span style="color: #008080;"> 51</span> <span style="color: #000000;">                    }
</span><span style="color: #008080;"> 52</span> <span style="color: #000000;">                }
</span><span style="color: #008080;"> 53</span>                 <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 54</span> <span style="color: #000000;">            }
</span><span style="color: #008080;"> 55</span>             <span style="color: #0000ff;">catch</span>
<span style="color: #008080;"> 56</span> <span style="color: #000000;">            {
</span><span style="color: #008080;"> 57</span>                 <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 58</span> <span style="color: #000000;">            }
</span><span style="color: #008080;"> 59</span> <span style="color: #000000;">        }
</span><span style="color: #008080;"> 60</span> 
<span style="color: #008080;"> 61</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span>
<span style="color: #008080;"> 62</span>         <span style="color: #808080;">///</span><span style="color: #008000;"> 重启系统服务
</span><span style="color: #008080;"> 63</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span>
<span style="color: #008080;"> 64</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;param name="serviceName"&gt;</span><span style="color: #008000;">系统服务名称</span><span style="color: #808080;">&lt;/param&gt;</span>
<span style="color: #008080;"> 65</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;returns&gt;&lt;/returns&gt;</span>
<span style="color: #008080;"> 66</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">bool</span> SystemServiceReStart(<span style="color: #0000ff;">string</span><span style="color: #000000;"> serviceName)
</span><span style="color: #008080;"> 67</span> <span style="color: #000000;">        {
</span><span style="color: #008080;"> 68</span>             <span style="color: #0000ff;">try</span>
<span style="color: #008080;"> 69</span> <span style="color: #000000;">            {
</span><span style="color: #008080;"> 70</span>                 <span style="color: #0000ff;">using</span> (<span style="color: #0000ff;">var</span> control = <span style="color: #0000ff;">new</span><span style="color: #000000;"> ServiceController(serviceName))
</span><span style="color: #008080;"> 71</span> <span style="color: #000000;">                {
</span><span style="color: #008080;"> 72</span>                     <span style="color: #0000ff;">if</span> (control.Status ==<span style="color: #000000;"> System.ServiceProcess.ServiceControllerStatus.Running)
</span><span style="color: #008080;"> 73</span> <span style="color: #000000;">                    {
</span><span style="color: #008080;"> 74</span> <span style="color: #000000;">                        control.Continue();
</span><span style="color: #008080;"> 75</span> <span style="color: #000000;">                    }
</span><span style="color: #008080;"> 76</span> <span style="color: #000000;">                }
</span><span style="color: #008080;"> 77</span>                 <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 78</span> <span style="color: #000000;">            }
</span><span style="color: #008080;"> 79</span>             <span style="color: #0000ff;">catch</span>
<span style="color: #008080;"> 80</span> <span style="color: #000000;">            {
</span><span style="color: #008080;"> 81</span>                 <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 82</span> <span style="color: #000000;">            }
</span><span style="color: #008080;"> 83</span> <span style="color: #000000;">        }
</span><span style="color: #008080;"> 84</span> 
<span style="color: #008080;"> 85</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span>
<span style="color: #008080;"> 86</span>         <span style="color: #808080;">///</span><span style="color: #008000;"> 返回服务状态
</span><span style="color: #008080;"> 87</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span>
<span style="color: #008080;"> 88</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;param name="serviceName"&gt;</span><span style="color: #008000;">系统服务名称</span><span style="color: #808080;">&lt;/param&gt;</span>
<span style="color: #008080;"> 89</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;returns&gt;</span><span style="color: #008000;">1:服务未运行  2:服务正在启动  3:服务正在停止  4:服务正在运行  5:服务即将继续  6:服务即将暂停  7:服务已暂停  0:未知状态</span><span style="color: #808080;">&lt;/returns&gt;</span>
<span style="color: #008080;"> 90</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> GetSystemServiceStatus(<span style="color: #0000ff;">string</span><span style="color: #000000;"> serviceName)
</span><span style="color: #008080;"> 91</span> <span style="color: #000000;">        {
</span><span style="color: #008080;"> 92</span>             <span style="color: #0000ff;">try</span>
<span style="color: #008080;"> 93</span> <span style="color: #000000;">            {
</span><span style="color: #008080;"> 94</span>                 <span style="color: #0000ff;">using</span> (<span style="color: #0000ff;">var</span> control = <span style="color: #0000ff;">new</span><span style="color: #000000;"> ServiceController(serviceName))
</span><span style="color: #008080;"> 95</span> <span style="color: #000000;">                {
</span><span style="color: #008080;"> 96</span>                     <span style="color: #0000ff;">return</span> (<span style="color: #0000ff;">int</span><span style="color: #000000;">)control.Status;
</span><span style="color: #008080;"> 97</span> <span style="color: #000000;">                }
</span><span style="color: #008080;"> 98</span> <span style="color: #000000;">            }
</span><span style="color: #008080;"> 99</span>             <span style="color: #0000ff;">catch</span>
<span style="color: #008080;">100</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">101</span>                 <span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #008080;">102</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">103</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">104</span> 
<span style="color: #008080;">105</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span>
<span style="color: #008080;">106</span>         <span style="color: #808080;">///</span><span style="color: #008000;"> 返回服务状态
</span><span style="color: #008080;">107</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span>
<span style="color: #008080;">108</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;param name="serviceName"&gt;</span><span style="color: #008000;">系统服务名称</span><span style="color: #808080;">&lt;/param&gt;</span>
<span style="color: #008080;">109</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;returns&gt;</span><span style="color: #008000;">1:服务未运行  2:服务正在启动  3:服务正在停止  4:服务正在运行  5:服务即将继续  6:服务即将暂停  7:服务已暂停  0:未知状态</span><span style="color: #808080;">&lt;/returns&gt;</span>
<span style="color: #008080;">110</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">string</span> GetSystemServiceStatusString(<span style="color: #0000ff;">string</span><span style="color: #000000;"> serviceName)
</span><span style="color: #008080;">111</span> <span style="color: #000000;">        {
</span><span style="color: #008080;">112</span>             <span style="color: #0000ff;">try</span>
<span style="color: #008080;">113</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">114</span>                 <span style="color: #0000ff;">using</span> (<span style="color: #0000ff;">var</span> control = <span style="color: #0000ff;">new</span><span style="color: #000000;"> ServiceController(serviceName))
</span><span style="color: #008080;">115</span> <span style="color: #000000;">                {
</span><span style="color: #008080;">116</span>                     <span style="color: #0000ff;">var</span> status = <span style="color: #0000ff;">string</span><span style="color: #000000;">.Empty;
</span><span style="color: #008080;">117</span>                     <span style="color: #0000ff;">switch</span> ((<span style="color: #0000ff;">int</span><span style="color: #000000;">)control.Status)
</span><span style="color: #008080;">118</span> <span style="color: #000000;">                    {
</span><span style="color: #008080;">119</span>                         <span style="color: #0000ff;">case</span> <span style="color: #800080;">1</span><span style="color: #000000;">:
</span><span style="color: #008080;">120</span>                             status = <span style="color: #800000;">"</span><span style="color: #800000;">服务未运行</span><span style="color: #800000;">"</span><span style="color: #000000;">;
</span><span style="color: #008080;">121</span>                             <span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #008080;">122</span>                         <span style="color: #0000ff;">case</span> <span style="color: #800080;">2</span><span style="color: #000000;">:
</span><span style="color: #008080;">123</span>                             status = <span style="color: #800000;">"</span><span style="color: #800000;">服务正在启动</span><span style="color: #800000;">"</span><span style="color: #000000;">;
</span><span style="color: #008080;">124</span>                             <span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #008080;">125</span>                         <span style="color: #0000ff;">case</span> <span style="color: #800080;">3</span><span style="color: #000000;">:
</span><span style="color: #008080;">126</span>                             status = <span style="color: #800000;">"</span><span style="color: #800000;">服务正在停止</span><span style="color: #800000;">"</span><span style="color: #000000;">;
</span><span style="color: #008080;">127</span>                             <span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #008080;">128</span>                         <span style="color: #0000ff;">case</span> <span style="color: #800080;">4</span><span style="color: #000000;">:
</span><span style="color: #008080;">129</span>                             status = <span style="color: #800000;">"</span><span style="color: #800000;">服务正在运行</span><span style="color: #800000;">"</span><span style="color: #000000;">;
</span><span style="color: #008080;">130</span>                             <span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #008080;">131</span>                         <span style="color: #0000ff;">case</span> <span style="color: #800080;">5</span><span style="color: #000000;">:
</span><span style="color: #008080;">132</span>                             status = <span style="color: #800000;">"</span><span style="color: #800000;">服务即将继续</span><span style="color: #800000;">"</span><span style="color: #000000;">;
</span><span style="color: #008080;">133</span>                             <span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #008080;">134</span>                         <span style="color: #0000ff;">case</span> <span style="color: #800080;">6</span><span style="color: #000000;">:
</span><span style="color: #008080;">135</span>                             status = <span style="color: #800000;">"</span><span style="color: #800000;">服务即将暂停</span><span style="color: #800000;">"</span><span style="color: #000000;">;
</span><span style="color: #008080;">136</span>                             <span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #008080;">137</span>                         <span style="color: #0000ff;">case</span> <span style="color: #800080;">7</span><span style="color: #000000;">:
</span><span style="color: #008080;">138</span>                             status = <span style="color: #800000;">"</span><span style="color: #800000;">服务已暂停</span><span style="color: #800000;">"</span><span style="color: #000000;">;
</span><span style="color: #008080;">139</span>                             <span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #008080;">140</span>                         <span style="color: #0000ff;">case</span> <span style="color: #800080;">0</span><span style="color: #000000;">:
</span><span style="color: #008080;">141</span>                             status = <span style="color: #800000;">"</span><span style="color: #800000;">未知状态</span><span style="color: #800000;">"</span><span style="color: #000000;">;
</span><span style="color: #008080;">142</span>                             <span style="color: #0000ff;">break</span><span style="color: #000000;">;
</span><span style="color: #008080;">143</span> <span style="color: #000000;">                    }
</span><span style="color: #008080;">144</span>                     <span style="color: #0000ff;">return</span><span style="color: #000000;"> status;
</span><span style="color: #008080;">145</span> <span style="color: #000000;">                }
</span><span style="color: #008080;">146</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">147</span>             <span style="color: #0000ff;">catch</span>
<span style="color: #008080;">148</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">149</span>                 <span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">未知状态</span><span style="color: #800000;">"</span><span style="color: #000000;">;
</span><span style="color: #008080;">150</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">151</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">152</span> 
<span style="color: #008080;">153</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span>
<span style="color: #008080;">154</span>         <span style="color: #808080;">///</span><span style="color: #008000;"> 安装服务
</span><span style="color: #008080;">155</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span>
<span style="color: #008080;">156</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;param name="stateSaver"&gt;&lt;/param&gt;</span>
<span style="color: #008080;">157</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;param name="filepath"&gt;&lt;/param&gt;</span>
<span style="color: #008080;">158</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> InstallService(IDictionary stateSaver, <span style="color: #0000ff;">string</span><span style="color: #000000;"> filepath)
</span><span style="color: #008080;">159</span> <span style="color: #000000;">        {
</span><span style="color: #008080;">160</span>             <span style="color: #0000ff;">try</span>
<span style="color: #008080;">161</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">162</span>                 <span style="color: #0000ff;">var</span> myAssemblyInstaller = <span style="color: #0000ff;">new</span><span style="color: #000000;"> AssemblyInstaller
</span><span style="color: #008080;">163</span> <span style="color: #000000;">                {
</span><span style="color: #008080;">164</span>                     UseNewContext = <span style="color: #0000ff;">true</span><span style="color: #000000;">,
</span><span style="color: #008080;">165</span>                     Path =<span style="color: #000000;"> filepath
</span><span style="color: #008080;">166</span> <span style="color: #000000;">                };
</span><span style="color: #008080;">167</span> <span style="color: #000000;">                myAssemblyInstaller.Install(stateSaver);
</span><span style="color: #008080;">168</span> <span style="color: #000000;">                myAssemblyInstaller.Commit(stateSaver);
</span><span style="color: #008080;">169</span> <span style="color: #000000;">                myAssemblyInstaller.Dispose();
</span><span style="color: #008080;">170</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">171</span>             <span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception ex)
</span><span style="color: #008080;">172</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">173</span>                 <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> Exception(<span style="color: #800000;">"</span><span style="color: #800000;">installServiceError/n</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> ex.Message);
</span><span style="color: #008080;">174</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">175</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">176</span> 
<span style="color: #008080;">177</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">bool</span> ServiceIsExisted(<span style="color: #0000ff;">string</span><span style="color: #000000;"> serviceName)
</span><span style="color: #008080;">178</span> <span style="color: #000000;">        {
</span><span style="color: #008080;">179</span>             ServiceController[] services =<span style="color: #000000;"> ServiceController.GetServices();
</span><span style="color: #008080;">180</span>             <span style="color: #0000ff;">return</span> services.Any(s =&gt; s.ServiceName ==<span style="color: #000000;"> serviceName);
</span><span style="color: #008080;">181</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">182</span> 
<span style="color: #008080;">183</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span>
<span style="color: #008080;">184</span>         <span style="color: #808080;">///</span><span style="color: #008000;"> 卸载服务
</span><span style="color: #008080;">185</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span>
<span style="color: #008080;">186</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;param name="filepath"&gt;</span><span style="color: #008000;">路径和文件名</span><span style="color: #808080;">&lt;/param&gt;</span>
<span style="color: #008080;">187</span>         <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> UnInstallService(<span style="color: #0000ff;">string</span><span style="color: #000000;"> filepath)
</span><span style="color: #008080;">188</span> <span style="color: #000000;">        {
</span><span style="color: #008080;">189</span>             <span style="color: #0000ff;">try</span>
<span style="color: #008080;">190</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">191</span>                 <span style="color: #008000;">//</span><span style="color: #008000;">UnInstall Service  </span>
<span style="color: #008080;">192</span>                 <span style="color: #0000ff;">var</span> myAssemblyInstaller = <span style="color: #0000ff;">new</span><span style="color: #000000;"> AssemblyInstaller
</span><span style="color: #008080;">193</span> <span style="color: #000000;">                {
</span><span style="color: #008080;">194</span>                     UseNewContext = <span style="color: #0000ff;">true</span><span style="color: #000000;">,
</span><span style="color: #008080;">195</span>                     Path =<span style="color: #000000;"> filepath
</span><span style="color: #008080;">196</span> <span style="color: #000000;">                };
</span><span style="color: #008080;">197</span>                 myAssemblyInstaller.Uninstall(<span style="color: #0000ff;">null</span><span style="color: #000000;">);
</span><span style="color: #008080;">198</span> <span style="color: #000000;">                myAssemblyInstaller.Dispose();
</span><span style="color: #008080;">199</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">200</span>             <span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception ex)
</span><span style="color: #008080;">201</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">202</span>                 <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> Exception(<span style="color: #800000;">"</span><span style="color: #800000;">unInstallServiceError/n</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> ex.Message);
</span><span style="color: #008080;">203</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">204</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">205</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">206</span> }</pre>
</div>
<p>接下来我们封装控制台的操作方法为了实现循环监听这里用了递归</p>
<div class="cnblogs_code">
<pre><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System;
</span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Diagnostics;
</span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System.IO;
</span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Linq;
</span><span style="color: #008080;"> 5</span> 
<span style="color: #008080;"> 6</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> AutoInstallUtil
</span><span style="color: #008080;"> 7</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 8</span>     <span style="color: #0000ff;">class</span><span style="color: #000000;"> Program
</span><span style="color: #008080;"> 9</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">10</span>         <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
</span><span style="color: #008080;">11</span> <span style="color: #000000;">        {
</span><span style="color: #008080;">12</span>             <span style="color: #0000ff;">try</span>
<span style="color: #008080;">13</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">14</span> <span style="color: #000000;">                ServerAction();
</span><span style="color: #008080;">15</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">16</span>             <span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception ex)
</span><span style="color: #008080;">17</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">18</span>                 Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">发生错误：{0}</span><span style="color: #800000;">"</span><span style="color: #000000;">, ex.Message);
</span><span style="color: #008080;">19</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">20</span> 
<span style="color: #008080;">21</span> <span style="color: #000000;">            Console.ReadKey();
</span><span style="color: #008080;">22</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">23</span> 
<span style="color: #008080;">24</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span>
<span style="color: #008080;">25</span>         <span style="color: #808080;">///</span><span style="color: #008000;"> 操作
</span><span style="color: #008080;">26</span>         <span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span>
<span style="color: #008080;">27</span>         <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> ServerAction()
</span><span style="color: #008080;">28</span> <span style="color: #000000;">        {
</span><span style="color: #008080;">29</span>             Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">请输入：1安装  2卸载</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #008080;">30</span>             <span style="color: #0000ff;">var</span> condition =<span style="color: #000000;"> Console.ReadLine();
</span><span style="color: #008080;">31</span>             <span style="color: #0000ff;">var</span> currentPath =<span style="color: #000000;"> Environment.CurrentDirectory;
</span><span style="color: #008080;">32</span>             <span style="color: #0000ff;">var</span> currentFileNameVshost =<span style="color: #000000;"> Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName).ToLower();
</span><span style="color: #008080;">33</span>             <span style="color: #0000ff;">var</span> currentFileName = currentFileNameVshost.Replace(<span style="color: #800000;">"</span><span style="color: #800000;">.vshost.exe</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">.exe</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #008080;">34</span>             <span style="color: #0000ff;">var</span> files =
<span style="color: #008080;">35</span> <span style="color: #000000;">                Directory.GetFiles(currentPath)
</span><span style="color: #008080;">36</span>                     .Select(o =&gt;<span style="color: #000000;"> Path.GetFileName(o).ToLower())
</span><span style="color: #008080;">37</span> <span style="color: #000000;">                    .ToList()
</span><span style="color: #008080;">38</span> <span style="color: #000000;">                    .Where(
</span><span style="color: #008080;">39</span>                         o =&gt;
<span style="color: #008080;">40</span>                             o !=<span style="color: #000000;"> currentFileNameVshost
</span><span style="color: #008080;">41</span>                             &amp;&amp; o !=<span style="color: #000000;"> currentFileName
</span><span style="color: #008080;">42</span>                             &amp;&amp; o.ToLower().EndsWith(<span style="color: #800000;">"</span><span style="color: #800000;">.exe</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #008080;">43</span>                             &amp;&amp; o != <span style="color: #800000;">"</span><span style="color: #800000;">installutil.exe</span><span style="color: #800000;">"</span>
<span style="color: #008080;">44</span>                             &amp;&amp; !o.ToLower().EndsWith(<span style="color: #800000;">"</span><span style="color: #800000;">.vshost.exe</span><span style="color: #800000;">"</span><span style="color: #000000;">))
</span><span style="color: #008080;">45</span> <span style="color: #000000;">                    .ToList();
</span><span style="color: #008080;">46</span>             <span style="color: #0000ff;">if</span> (files.Count == <span style="color: #800080;">0</span><span style="color: #000000;">)
</span><span style="color: #008080;">47</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">48</span>                 Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">未找到可执行文件，请确认当前目录有需要安装的服务程序</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #008080;">49</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">50</span>             <span style="color: #0000ff;">else</span>
<span style="color: #008080;">51</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">52</span>                 Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">找到目录有如下可执行文件，请选择需要安装或卸载的文件序号：</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #008080;">53</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">54</span>             <span style="color: #0000ff;">int</span> i = <span style="color: #800080;">0</span><span style="color: #000000;">;
</span><span style="color: #008080;">55</span>             <span style="color: #0000ff;">foreach</span> (<span style="color: #0000ff;">var</span> file <span style="color: #0000ff;">in</span><span style="color: #000000;"> files)
</span><span style="color: #008080;">56</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">57</span>                 Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">序号：{0}  文件名：{1}</span><span style="color: #800000;">"</span><span style="color: #000000;">, i, file);
</span><span style="color: #008080;">58</span>                 i++<span style="color: #000000;">;
</span><span style="color: #008080;">59</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">60</span>             <span style="color: #0000ff;">var</span> serviceFileIndex =<span style="color: #000000;"> Console.ReadLine();
</span><span style="color: #008080;">61</span>             <span style="color: #0000ff;">var</span> servicePathName = currentPath + <span style="color: #800000;">"</span><span style="color: #800000;">\\</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> files[Convert.ToInt32(serviceFileIndex)];
</span><span style="color: #008080;">62</span>             <span style="color: #0000ff;">if</span> (condition == <span style="color: #800000;">"</span><span style="color: #800000;">1</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #008080;">63</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">64</span>                 SystemServices.InstallService(<span style="color: #0000ff;">null</span><span style="color: #000000;">, servicePathName);
</span><span style="color: #008080;">65</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">66</span>             <span style="color: #0000ff;">else</span>
<span style="color: #008080;">67</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">68</span> <span style="color: #000000;">                SystemServices.UnInstallService(servicePathName);
</span><span style="color: #008080;">69</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">70</span>             Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">**********本次操作完毕**********</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #008080;">71</span> <span style="color: #000000;">            ServerAction();
</span><span style="color: #008080;">72</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">73</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">74</span> }</pre>
</div>
<p>到此为止简单的安装程序就写完了，为了醒目我选了个红色的西红柿来做为图标，这样显示些<img src="http://images2015.cnblogs.com/blog/61258/201509/61258-20150910223326872-1758742989.png" alt="" /></p>
<h2>源码和程序</h2>
<p><a href="http://pan.baidu.com/s/1qWqXG4W" target="_blank">http://pan.baidu.com/s/1qWqXG4W</a>&nbsp; &nbsp; 提取码：piq4</p>
<p>&nbsp;</p></div><div id="MySignature"></div>
<div class="clear"></div>
<div id="blog_post_info_block">
<div id="BlogPostCategory"></div>
<div id="EntryTag"></div>
<div id="blog_post_info">
</div>
<div class="clear"></div>
<div id="post_next_prev"></div>
</div>


		</div>
		<div class = "postDesc">posted @ <span id="post-date">2015-09-10 22:39</span> <a href='http://www.cnblogs.com/hantianwei/'>韩天伟</a> 阅读(<span id="post_view_count">...</span>) 评论(<span id="post_comment_count">...</span>)  <a href ="http://i.cnblogs.com/EditPosts.aspx?postid=4799434" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(4799434);return false;">收藏</a></div>
	</div>
	<script type="text/javascript">var allowComments=true,isLogined=false,cb_blogId=55427,cb_entryId=4799434,cb_blogApp=currentBlogApp,cb_blogUserGuid='7e95bab1-9f2d-de11-9510-001cf0cd104b',cb_entryCreatedDate='2015/9/10 22:39:00';loadViewCount(cb_entryId);</script>
	
</div><!--end: topics 文章、评论容器-->
</div><a name="!comments"></a><div id="blog-comments-placeholder"></div><script type="text/javascript">var commentManager = new blogCommentManager();commentManager.renderComments(0);</script>
<div id='comment_form' class='commentform'>
<a name='commentform'></a>
<div id='divCommentShow'></div>
<div id='comment_nav'><span id='span_refresh_tips'></span><a href='javascript:void(0);' onclick='return RefreshCommentList();' id='lnk_RefreshComments' runat='server' clientidmode='Static'>刷新评论</a><a href='#' onclick='return RefreshPage();'>刷新页面</a><a href='#top'>返回顶部</a></div>
<div id='comment_form_container'></div>
<div class='ad_text_commentbox' id='ad_text_under_commentbox'></div>
<div id='ad_t2'></div>
<div id='opt_under_post'></div>
<div id='ad_c1' class='c_ad_block'></div>
<div id='under_post_news'></div>
<div id='under_post_kb'></div>
<div id='HistoryToday' class='c_ad_block'></div>
<script type='text/javascript'>
$(function () {
    setTimeout(function () { incrementViewCount(cb_entryId); }, 50);
    deliverAdT2();
    deliverAdC1();    
    loadNewsAndKb();
    loadBlogSignature();
    LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);
    GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate);
    loadOptUnderPost();
    GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);    
});
</script>
</div>


	</div><!--end: forFlow -->
	</div><!--end: mainContent 主体内容容器-->

	<div id="sideBar">
		<div id="sideBarMain">
			
<!--done-->
<div class="newsItem">
<h3 class="catListTitle">公告</h3>
	<div id="blog-news"></div><script type="text/javascript">loadBlogNews();</script>
</div>

			<div id="blog-calendar" style="display:none"></div><script type="text/javascript">loadBlogDefaultCalendar();</script>
			
			<div id="leftcontentcontainer">
				<div id="blog-sidecolumn"></div><script type="text/javascript">loadBlogSideColumn();</script>
			</div>
			
		</div><!--end: sideBarMain -->
	</div><!--end: sideBar 侧边栏容器 -->
	<div class="clear"></div>
	</div><!--end: main -->
	<div class="clear"></div>
	<div id="footer">
		
<!--done-->
Copyright &copy;2016 韩天伟
	</div><!--end: footer -->
</div><!--end: home 自定义的最大容器 -->
<!--PageEndHtml Block Begin-->
<!-- Baidu Button BEGIN -->
<script type="text/javascript" id="bdshare_js" data="type=slide&amp;img=5&amp;uid=147952" ></script>
<script type="text/javascript" id="bdshell_js"></script>
<script type="text/javascript">
	document.getElementById("bdshell_js").src = "http://share.baidu.com/static/js/shell_v2.js?t=" + new Date().getHours();
</script>
<!-- Baidu Button END -->
<center><script src="http://s9.cnzz.com/stat.php?id=3298731&web_id=3298731&show=pic" language="JavaScript"></script>
</center>
<!--PageEndHtml Block End-->
</body>
</html>
